package com.quan.server.service;

import com.mark.util.CommonUtil;
import com.quan.server.beans.Customer;
import com.quan.server.beans.Income;
import com.quan.server.beans.InvitingRecord;
import com.quan.server.beans.TaobaoOrder;
import com.quan.server.helper.MyAppHelper;
import com.quan.server.jpa.CustomerRepository;
import com.quan.server.jpa.InvitingRecordRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Component
@Path("/androidProxyService")
public class AndroidProxyService {
    private static Logger logger = LoggerFactory.getLogger(AndroidProxyService.class);
    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private MyAppHelper myAppHelper;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response post(Customer customer) {
        try {
            customer.id = CommonUtil.createRandomId();
            customer = customerRepository.save(customer);
            return Response.status(Response.Status.CREATED).entity(customer).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response delete(@PathParam("id") String id) {
        try {
            customerRepository.delete(id);
            return Response.status(Response.Status.NO_CONTENT).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response put(Customer customer) {
        try {
            if (CommonUtil.isEmpty(customer.id)) {
                customer.id = CommonUtil.createRandomId();
            }
            customer = customerRepository.save(customer);
            return Response.ok(customer).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(@PathParam("id") String id) {
        try {
            Customer customer = customerRepository.findOne(id);
            if (customer == null) {
                return Response.status(Response.Status.NO_CONTENT).build();
            } else {
                return Response.ok(customer).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAll() {
        try {
            List<Customer> customerList = customerRepository.findAll();
            return Response.ok(customerList).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("branch")
    public Response post(List<Customer> customerList) {
        for (Customer customer : customerList) {
            post(customer);
        }
        return Response.ok().build();
    }

    @GET
    @Path("/getIfCustomerByWeixinId")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getIfCustomerByWeixinId(@QueryParam("weixinId") String weixinId) {
        try {
            Customer customer = customerRepository.findCustomerByWeixinId(weixinId);
            if (customer == null) {
                customer = new Customer();
                customer.id = CommonUtil.createRandomId();
                customer.weixinId = weixinId;
                customer = customerRepository.save(customer);
            }
            if (customer != null) {
                return Response.ok(customer).build();
            } else {
                return Response.status(Response.Status.NO_CONTENT).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/handleMessage")
    @Produces(MediaType.APPLICATION_JSON)
    public Response handleMessage(@QueryParam("weixinId") String weixinIdSender, @QueryParam("message") String message, @QueryParam("weixinIdReceiver") @DefaultValue("") String weixinIdReceiver) {
        try {
            String messageReturn = myAppHelper.handleMessage(weixinIdSender, message);
            if (weixinIdReceiver != null && !weixinIdReceiver.equals("")) {
                myAppHelper.getIfInvitingRecordByWeixinIdSenderAndWeixinIdReceiver(weixinIdSender, weixinIdReceiver);
            }
            if (messageReturn != null) {
                return Response.ok(messageReturn).build();
            } else {
                return Response.status(Response.Status.NO_CONTENT).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/getIfInvitingRecordByWeixinIdSenderAndWeixinIdReceiver")
    public Response getIfInvitingRecordByWeixinIdSenderAndWeixinIdReceiver(@QueryParam("weixinIdSender") String weixinIdSender, @QueryParam("weixinIdReceiver") String weixinIdReceiver) {
        try {
            InvitingRecord invitingRecord = myAppHelper.getIfInvitingRecordByWeixinIdSenderAndWeixinIdReceiver(weixinIdSender, weixinIdReceiver);
            return Response.ok(invitingRecord).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/importTaobaoOrder")
    public Response importTaobaoOrder(TaobaoOrder taobaoOrder) {
        try {
            myAppHelper.importTaobaoOrder(taobaoOrder);
            return Response.ok().build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/getIfIncomeByCustomerId")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getIfIncomeByCustomerId(@QueryParam("customerId") String customerId) {
        try {
            Income income = myAppHelper.getIfIncomeByCustomerId(customerId);
            return Response.ok(income).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }
}
